//
//  newCatchController.m
//  HelloWordPrimer
//
//  Created by erik on 2009-10-15.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "NewCatchController.h"
#import "FishCatchAnnotation.h"
#import "Catch.h"
#import "catchViewController.h"



@implementation NewCatchController
@synthesize navController;
@synthesize actionSheet;
@synthesize weight;
@synthesize speciesValue;
@synthesize lblWeight;
@synthesize lblSpecies;
@synthesize managedObjectContext;
@synthesize location;


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	[self setTitle:@"New Catch"];
	UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(saveCatch:)];
	doneButton.style = UIBarButtonItemStyleDone;
	self.navigationItem.rightBarButtonItem = doneButton;
	
	[[UIDevice currentDevice]setOrientation:UIInterfaceOrientationPortrait];
	[doneButton release];
}

-(void)saveCatch:(id)sender{
	 //spara all info här....	
	if(managedObjectContext)
	{
		if (!location) {
			NSLog(@"No location provided");
			return;
		}
		
		// Persistence test
		Catch *catch = (Catch *) [NSEntityDescription insertNewObjectForEntityForName:@"Catch" inManagedObjectContext:managedObjectContext];
		CLLocationCoordinate2D coordinate = [location coordinate]; // Get coordinates from current location
		catch.longitude = [NSNumber numberWithDouble:coordinate.longitude];
		catch.latitude = [NSNumber numberWithDouble:coordinate.latitude];

		catch.catchDate = [NSDate date];
		catch.species = [self speciesValue];
		catch.weight = [NSNumber numberWithDouble:[self.weight doubleValue]];
		
		// Add annotation
		FishCatchAnnotation *catchAnno = [[FishCatchAnnotation alloc] initWithCoordinate:coordinate];
		[catchAnno setTitle:speciesValue];
		[catchAnno setSubtitle:[[NSString alloc]initWithFormat:@"%@ Kg",weight]];
		
		[mapView addAnnotation:catchAnno];
		//[mapView addAnnotation:catchAnno];
		
		NSLog(@"Try to save one catch at current location");
		NSError *error;
		if (![managedObjectContext save:&error]) {
			NSLog(@"Ett fel uppstod!!");
			UIAlertView *theError = [[UIAlertView alloc] initWithTitle: @"Save error"
															   message: @"Error saving data to persisten store"
															  delegate: self cancelButtonTitle: @"Ok"
													 otherButtonTitles: nil];
			[theError show];
			[theError release];
		}
		[self.navigationController popViewControllerAnimated:YES];
	}
	else{
		UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"No context" message:@"No context available" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[alert show];
		[alert release];
	}	
}

-(IBAction)takePhoto:(id)sender{
	if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
		UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
		imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
		[self presentModalViewController:imagePicker animated:YES];
		[imagePicker release];
	}
	else if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
		UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
		//använd UIImagePickerControllerSourceTypeSavedPhotosAlbum eller UIImagePickerControllerSourceTypeCamera
		//på devicen!!
		imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
		[self presentModalViewController:imagePicker animated:YES];
		[imagePicker release];
	}
}

- (void)viewWillDisappear:(BOOL)animated{
	[[self navController] setToolbarHidden:NO animated:YES];
	[[self navController] setNavigationBarHidden:YES animated:YES];
}

- (void)viewWillAppear:(BOOL)animated{
	[[self navController] setToolbarHidden:YES animated:YES];
	[[self navController] setNavigationBarHidden:NO animated:YES];
}

-(void)imagePickerController:(UIImagePickerController *)picker
	   didFinishPickingImage:(UIImage *)image
				 editingInfo:(NSDictionary *)editingInfo{
	[self useImage:image];
	[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{	
	[[picker parentViewController]dismissModalViewControllerAnimated:YES];
}

-(void)useImage:(UIImage *)usedImage{
	//hantera bilden som valdes...
	UIAlertView *imageView = [[UIAlertView alloc]initWithTitle:@"Image" message:@"You chose the a image!" delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
	[imageView show];
	[imageView release];
}

-(IBAction)setWeightValue:(id)sender{
	
	UIActionSheet *speciesSheet = [[UIActionSheet alloc] initWithTitle:@"Weight"
															  delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil
													 otherButtonTitles:@"Dummy",@"Dummy",@"Dummy",@"Dummy", nil];
	[self setActionSheet:speciesSheet];
	
	speciesSheet.actionSheetStyle = UIActionSheetStyleDefault;
	WeightPickerController *weightPickerView = [[WeightPickerController alloc]initWithNibName:@"WeightPickerView" bundle:nil];
	
	[weightPickerView setActionSheet:speciesSheet];
	[speciesSheet addSubview:weightPickerView.view];      
	[speciesSheet showInView:self.view]; 	
	
	[speciesSheet release];
	[(WeightPickerController *)weightPickerView setParentViewController:self];
}



-(IBAction)setSpecies:(id)sender{
	
	UIActionSheet *speciesSheet = [[UIActionSheet alloc] initWithTitle:@"Species"
															 delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil
													otherButtonTitles:@"Dummy",@"Dummy",@"Dummy",@"Dummy", nil];
	[self setActionSheet:speciesSheet];
	
	speciesSheet.actionSheetStyle = UIActionSheetStyleDefault;
	SpeciesPickerViewController *picker = [[SpeciesPickerViewController alloc]initWithNibName:@"SpeciesPickerView" bundle:nil];
	
	[picker setActionSheet:speciesSheet];
	[speciesSheet addSubview:picker.view];      
	[speciesSheet showInView:self.view]; 	
	
	[speciesSheet release];
	[picker setParentViewController:self];
	//får inte släppa denna av någon anledning...
	//[pickerView release];
}

-(void)changeLblSpecies:(NSString *)species{
	self.speciesValue = species;
	[lblSpecies setFont:[UIFont fontWithName:@"Arial" size:30]];
	[lblSpecies setText: [[NSString alloc]initWithFormat:@"Species: %@",species]];	
}

-(void)changeLblWeight:(NSString *)theWeight{
	[self setWeight:theWeight];
	
	[lblWeight setFont:[UIFont fontWithName:@"Arial" size:30]];
	[lblWeight setText: [[NSString alloc]initWithFormat:@"Weight: %@ Kilo",theWeight]];	
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
	[navController release];
	[actionSheet release];
	[speciesValue release];
	[weight release];
}


@end
